﻿using WPFExcelReport;
using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class TICC_SRULES
    {
        public TICC_SRULES()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_SRULES();
            DataContext = MV;
            ART = @"C:\IntexReport\Data\ITX\EXCEL";
            VAR = "ITXEX";
            InitializeComponent();
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            base.Window_Loaded(sender, e);
        }
        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            if (EXAUF.Length <6)
                return;
            dwData = TBMain.NewRow();
            dwData["JOBNUM"] = EXAUF.Substring(24, 6);
            SResult = TMV.QSR02(dwData);
            MergeData(SResult, false);
            base.OnEXAUFChg(vlaue);
        } 
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            SResult = TMV.QueryICC_SRULES(dwData, 9999);
            MergeData(SResult, true, true);
            base.QueryCommand(sender, e);
        }

        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            dwData["RULEID"] = VSDDAT.SD22.GETNEXT("0022", "SR", "  ", 1, 4);
            TBMain.Rows.InsertAt(dwData, 0);
            TMV.InsertICC_SRULES(dwData, app.company);
            base.NewCommand(sender, e);
        }


        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.DSRULES02(null);
            dwData = TBMain.Clone().NewRow();
            TMV.SetUpNewRow(ref dwData);
            foreach (DataRow row in TBMain.Rows)
            {
                dwData.ItemArray = row.ItemArray;
                TMV.InsertICC_SRULES(dwData,app.company);
            }
            //TMV.UpdateICC_SRULES(dwMaster, 0);
            base.SaveCommand(sender, e);
        }
        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;

            dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray;
            dwData["RULEID"] = VSDDAT.SD22.GETNEXT("0022", "SR", "  ", 1, 4);
            TBMain.Rows.InsertAt(dwData, 0);
            TMV.InsertICC_SRULES(dwData, app.company);
            base.CopyCommand(sender, e);
        }

        private void LoadExcelCommand(object sender, RoutedEventArgs e)
        {
            cDR.PRINT(null);
            if (cDR.SetExchange == null)
                return;
            if (cDR.SetExchange.Tables.Count == 0)
                return;
            foreach(DataRow row in cDR.SetExchange.Tables[0].Rows)
            {
                foreach(DataColumn c in cDR.SetExchange.Tables[0].Columns)
                {
                    row[c] = Helper.ConvertGB2312To8859P1(row[c].ToString());
                }
            }
            MergeData(cDR.SetExchange, true, true);
        }
    }
}